﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web;
using FwProject.Comm.Sms;
using Abp.RemoteEventBus;
using Abp.Domain.Repositories;
using FwProject.Authorization.Users;
using System.Linq;
using Abp.UI;
using FwProject.Comm;

namespace FwProject.Sms
{
    public class AliyunSmsSender : IAliyunSmsSender
    {
        private readonly IRepository<User,long> _RepositoryUser;
        public IRemoteEventBus remoteEventBus { get; set; }//缓存对象

        public AliyunSmsSender(IRepository<User, long> RepositoryUser) {
            remoteEventBus = NullRemoteEventBus.Instance;
            _RepositoryUser = RepositoryUser;
        }

           /// <summary>
           /// 发送短信
           /// </summary>
           public async Task<(bool success, string response)> Send(SmsObject sms)
           {

               var model= (await _RepositoryUser.GetAllListAsync()).Where(o => o.Mobile == sms.Mobile).FirstOrDefault();
              if (sms.TemplateCode == TemplateCode.用户注册) {
                if (model != null)
                    throw new UserFriendlyException("该手机已注册");
                     
                }
             if (sms.TemplateCode != TemplateCode.用户注册)
                {
                    if (model == null)
                        throw new UserFriendlyException("该手机号码不存在");

                }

             var catcheventDate = new RemoteEventData(RemoteTypeHelp.TypeSmsObject)
                {
                    Data = { [RemoteTypeHelp.SmsObject] = sms }
                };
                remoteEventBus.Publish(RemoteTypeHelp.TopicSms, catcheventDate);


            return (true, response:"短信发送");
           }

    }
 }




